﻿using PaddleOCRSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Library.Helper
{
    /// <summary>
    /// 图像识别类
    /// </summary>
    public class OCRHelp
    {
        private static readonly string[] _bmpFilters = new string[] { ".bmp", ".jpg", ".jpeg", ".tiff", ".tif", ".png" };
        private static readonly string _fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        private static PaddleOCREngine engine;
        //OCRResult lastocrResult;
        /// <summary>
        /// 初始化ocr engine
        /// </summary>
        public static PaddleOCREngine InitORC()
        {
            //自带轻量版中英文模型V3模型
            OCRModelConfig config = null;

            //服务器中英文模型
            //OCRModelConfig config = new OCRModelConfig();
            //string root = System.IO.Path.GetDirectoryName(typeof(OCRModelConfig).Assembly.Location);
            //string modelPathroot = root + @"\inferenceserver";
            //config.det_infer = modelPathroot + @"\ch_ppocr_server_v2.0_det_infer";
            //config.cls_infer = modelPathroot + @"\ch_ppocr_mobile_v2.0_cls_infer";
            //config.rec_infer = modelPathroot + @"\ch_ppocr_server_v2.0_rec_infer";
            //config.keys = modelPathroot + @"\ppocr_keys.txt";

            //英文和数字模型
            //OCRModelConfig config = new OCRModelConfig();
            //string root = System.IO.Path.GetDirectoryName(typeof(OCRModelConfig).Assembly.Location);
            //string modelPathroot = root + @"\en";
            //config.det_infer = modelPathroot + @"\ch_PP-OCRv2_det_infer";
            //config.cls_infer = modelPathroot + @"\ch_ppocr_mobile_v2.0_cls_infer";
            //config.rec_infer = modelPathroot + @"\en_number_mobile_v2.0_rec_infer";
            //config.keys = modelPathroot + @"\en_dict.txt";

            //英文和数字模型V3
            //OCRModelConfig config = new OCRModelConfig();
            //string root = System.IO.Path.GetDirectoryName(typeof(OCRModelConfig).Assembly.Location);
            //string modelPathroot = root + @"\en_v3";
            //config.det_infer = modelPathroot + @"\en_PP-OCRv3_det_infer";
            //config.cls_infer = modelPathroot + @"\ch_ppocr_mobile_v2.0_cls_infer";
            //config.rec_infer = modelPathroot + @"\en_PP-OCRv3_rec_infer";
            //config.keys = modelPathroot + @"\en_dict.txt";


            //OCR参数
            OCRParameter oCRParameter = new OCRParameter();
            oCRParameter.numThread = 6;//预测并发线程数
            oCRParameter.Enable_mkldnn = 1;//web部署该值建议设置为0,否则出错，内存如果使用很大，建议该值也设置为0.
            oCRParameter.cls = 1; //是否执行文字方向分类；默认false
            oCRParameter.det = 1;//是否开启方向检测，用于检测识别180旋转
            oCRParameter.use_angle_cls = 1;//是否开启方向检测，用于检测识别180旋转
            oCRParameter.det_db_score_mode = 1;//是否使用多段线，即文字区域是用多段线还是用矩形，
            oCRParameter.UnClipRatio = 8.6f;

            oCRParameter.MaxSideLen = 960;
            //初始化OCR引擎
            engine = new PaddleOCREngine(config, oCRParameter);
            return engine;
        }
    }
}
